package com.arangodb.arangodemo.service;

import com.alibaba.fastjson.JSON;
import com.arangodb.arangodemo.arangoEntity.UserInfo;
import com.arangodb.arangodemo.reposity.UserInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoRepository userInfoRepository;

    public void get() {
        Iterable<UserInfo> userInfoRepositoryAll = userInfoRepository.findAll();
        //判断没有age的加上
        Iterator<UserInfo> iterator = userInfoRepositoryAll.iterator();
        while (iterator.hasNext()) {
            UserInfo u = iterator.next();

            if (u.getAge() == null) {
                int age =  (int) (Math.random() * (60 - 15) + 15);
//                System.out.println(u.getAge()+"-->"+age);
                u.setAge(age);


            }   System.out.println(u.getGender());
            if(u.getGender() == null){
                int gender = (int) (Math.random()*2);
//                System.out.println(u.getGender());
                u.setGender(gender);

            }
            if(u.getuId() == null){
                u.setuId(u.getUserId().toString());
                userInfoRepository.save(u);
            }

        }
        userInfoRepositoryAll = userInfoRepository.findAll();
        System.out.println(JSON.toJSONString(userInfoRepositoryAll));
    }
}
